package com.jie.jie.xiaohua.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ant.liao.GifView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jie.jie.R;
import com.jie.jie.xiaohua.model.XiaohuaBean;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jie on 2017/1/24.
 */

public class XiaohuaRvAdapter extends RecyclerView.Adapter<XiaohuaRvAdapter.ViewHolder> {

    private Context context;
    private List<XiaohuaBean.ResultBean.DataBean> datas;

    public XiaohuaRvAdapter(Context context, List<XiaohuaBean.ResultBean.DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.xiaohua_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.xiaohuaItemCon.setText(datas.get(position).getContent());
        holder.xiaohuaItemTime.setText(datas.get(position).getUpdatetime());
        if (datas.get(position).getUrl() != null && !"".equals(datas.get(position).getUrl())) {
            String url = datas.get(position).getUrl();
            if (url.substring(url.lastIndexOf("."), url.length()).equals("gif")) {
                try {

                    URL url1 = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    conn.connect();
                    if (conn.getResponseCode() == 200) {
                        holder.xiaohuaitem_gif.setGifImage(conn.getInputStream());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
            } else {
                Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.xiaohuaImg);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    XiaohuaActivity xiaohuaActivity = (XiaohuaActivity) context;
                    Intent intent = new Intent(xiaohuaActivity, QutuActivity.class);
                    intent.putExtra("titl1e", datas.get(position).getContent());
                    intent.putExtra("url", datas.get(position).getUrl());
                    xiaohuaActivity.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.xiaohua_item_con)
        TextView xiaohuaItemCon;
        @Bind(R.id.xiaohua_item_time)
        TextView xiaohuaItemTime;

        @Bind(R.id.xiaohua_item_img)
        ImageView xiaohuaImg;
        @Bind(R.id.xiaohua_item_gif)
        GifView xiaohuaitem_gif;
        ViewHolder(View view) {

            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
